package es.deusto.ingenieria.sd.tralala.identServer;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.util.HashMap;


@SuppressWarnings("serial")
public class ServerIdent extends UnicastRemoteObject implements IServerIdent  {
	private HashMap<String, String> hm; // Nombre, password
	
	public HashMap<String, String> getHm() {
		return hm;
	}
	public void setHm(HashMap<String, String> hm) {
		this.hm = hm;
	}
	
	
		
	
	
	public void setUsuario(String nombre,String contrasena){
		
		hm.put(nombre, contrasena);
		
	}
	protected ServerIdent() throws RemoteException {
		super();
		hm=new HashMap<String,String>();
		this.setUsuario("Ramon", "0000");
		this.setUsuario("Juan", "1234");
		
		
	}
	public void registrarRMI(String name){
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}

		

		try {
			IServerIdent objServidor = new ServerIdent();
			Naming.rebind(name, objServidor);
			System.out.println("* Server '" + name + "' activo y esperando...");
			
		} catch (Exception e) {
			System.err.println("- Exception running the server: " + e.getMessage());
			e.printStackTrace();
		}
		
	}
	

	
	public boolean Ilogin(String nombreUsuario, String pass) throws RemoteException {
		System.out.println("Nombre "+nombreUsuario+" pass " +pass);
		boolean b = false;
		if( hm.containsKey(nombreUsuario))
		  if(hm.get(nombreUsuario).equals(pass))
			{b= true;}
		System.out.println("* Login de Usuario: "+nombreUsuario + " password: "+pass+" boolean: "+b);
		return b;
	}

	
	public void registro(String nombreUsuario,String pass) throws RemoteException {
		hm.put(nombreUsuario, pass);
	}

	public static void main(String[] args) {
		
		if (args.length != 3) {
			System.out.println("usage: java [policy] [codebase] server.Server [host] [port] [server]");
			System.exit(0);
		}
		String name = "//" + args[0] + ":" + args[1] + "/" + args[2];
		ServerIdent servidorIdentificacion;
		try {
			servidorIdentificacion = new ServerIdent();
			servidorIdentificacion.registrarRMI(name);
			
			
			System.out.println(servidorIdentificacion.Ilogin("Ramon", "0000"));
			System.out.println(servidorIdentificacion.Ilogin("Juan", "pass"));
			
			
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	
		

		
	}

}
